#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include "sql/sql_api.h"

using namespace std;

WriteDictDatabase::WriteDictDatabase(const string fcInSqlitePath)
{
	char* mpErrorMsg = NULL;
	int rc;

	//------------------
	rc = sqlite3_open(fcInSqlitePath.c_str(), &mpSqlite);

	if( rc != SQLITE_OK ){
		cout << "数据库打开失败: " << sqlite3_errmsg(mpSqlite) << endl;
		return;
	}
	if(mcbDebug) {
		cout << "数据库打开成功。" << endl;
	}

	//------------------
	rc = sqlite3_exec(mpSqlite, "BEGIN;", 0, 0, &mpErrorMsg);
	if(rc != SQLITE_OK) {
		cout << "显示事务开启失败！" << endl;
		sqlite3_free(mpErrorMsg);
		return;
	}
	if(mcbDebug) {
		cout << "显示事务开启成功。" << endl;
	}
}

WriteDictDatabase::~WriteDictDatabase()
{
	char* mpErrorMsg = NULL;
	int rc;
	
	//------------------
	rc = sqlite3_exec(mpSqlite, "COMMIT;", 0, 0, &mpErrorMsg);
	if(rc != SQLITE_OK) {
		cout << "显示事务关闭失败！" << endl;
		sqlite3_free(mpErrorMsg);
		return;
	}
	if(mcbDebug) {
		cout << "显示事务关闭成功。" << endl;
	}

	//------------------
	//关闭数据库
	rc = sqlite3_close(mpSqlite);
	if(rc != SQLITE_OK) {
		cout << "关闭数据库失败！" << endl;
		return;
	}
	if(mcbDebug) {
		cout << "关闭数据库成功。" << endl;
	}
}


int WriteDictDatabase::create_dict_table(const string fcInTableName, const bool fcbInDeleteTable)
{
	char* mpErrorMsg = NULL;
	int rc;
	//------------------
	if(fcbInDeleteTable) {
		//删除表格
		const string dropDbsql = "DROP TABLE " + fcInTableName;
		if(mcbDebug) {
			cout << dropDbsql << endl;
		}

		rc = sqlite3_exec(mpSqlite, dropDbsql.c_str(), 0, 0, &mpErrorMsg);
		if (rc != SQLITE_OK) {
			cout << "删除表格失败！" << dropDbsql << endl;
			sqlite3_free(mpErrorMsg);
			//return -1;
		}
		else {
			if(mcbDebug) {
				cout << "删除表格成功。" << endl;
			}
		}
	}
	
	//------------------
	//创建表格
	const string createDbsql = "CREATE TABLE " + fcInTableName + "(word TEXT NOT NULL,phoneme TEXT NOT NULL)";
	if(mcbDebug) {
		cout << createDbsql << endl;
	}

	rc = sqlite3_exec(mpSqlite, createDbsql.c_str(), 0, 0, &mpErrorMsg);
	if (rc != SQLITE_OK) {
		cout << "创建表格失败！" << endl;
		sqlite3_free(mpErrorMsg);
		return -1;
	}
	if(mcbDebug) {
		cout << "创建表格成功。" << endl;
	}

	//------------------
	//添加索引
	const string createUniqueIndex = "CREATE UNIQUE INDEX ui_" + fcInTableName + " on " + fcInTableName + " (word)";
	if(mcbDebug) {
		cout << createUniqueIndex << endl;
	}

	rc = sqlite3_exec(mpSqlite, createUniqueIndex.c_str(), 0, 0, &mpErrorMsg);
	if (rc != SQLITE_OK) {
		cout << "添加索引失败！" << endl;
		sqlite3_free(mpErrorMsg);
		return -1;
	}
	if(mcbDebug) {
		cout << "添加索引成功。" << endl;
	}
	return 0;
}


bool WriteDictDatabase::add_sqlite_dict(const string fcInTableName, const bool fcbInDeleteTable, const string fcInKey, const string fcInValue)
{
	char* mpErrorMsg = NULL;
	int rc;

	//------------------
	//创建表
	auto tIterName = find(mvTableNames.begin(), mvTableNames.end(), fcInTableName);
	if(tIterName == mvTableNames.end()) {
		rc = create_dict_table(fcInTableName, fcbInDeleteTable);
		if (rc != SQLITE_OK) {
			cout << "创建表失败！" << endl;
			return false;
		}
		else {
			if(mcbDebug) {
				cout << "创建表成功。" << endl;
			}
			mvTableNames.push_back(fcInTableName);
		}
	}

	//------------------
	//插入数据
	const string insertsql = "INSERT INTO " + fcInTableName + " (word,phoneme) VALUES (\"" + fcInKey + "\",\"" + fcInValue + "\")";
	if(mcbDebug) {
		cout << insertsql << endl;
	}

	rc = sqlite3_exec(mpSqlite, insertsql.c_str(), 0, 0, &mpErrorMsg);
	if (rc != SQLITE_OK) {
		cout << "插入数据失败！" << fcInKey << "->" << fcInValue << "\t" << insertsql << endl;
		sqlite3_free(mpErrorMsg);
		return false;
	}
	if(mcbDebug) {
		//cout << "插入数据成功。" << endl;
	}
	return true;
}
